package com.lagou.framework;

import com.lagou.framework.config.XmlServerConfig;
import com.lagou.framework.config.XmlServerResolver;
import com.lagou.framework.container.Service;
import com.lagou.framework.coyote.EndPoint;

import java.io.IOException;
import java.util.Objects;

/**
 * @author srh
 * @date 2020/03/31
 **/
public class BootStrap {

    private static final String DEFAULT_PATH = "conf/server.xml";

    private void init() throws Exception {
        // 解析server.xml
        XmlServerConfig xmlServerConfig = parseXmlServer();
        // 初始化 engine 容器组件 以及 Connector 连接器组件
        Service.getInstance().init(xmlServerConfig.getService());
    }

    private XmlServerConfig parseXmlServer() throws Exception {
        XmlServerResolver resolver = new XmlServerResolver();
        return resolver.read(Objects.requireNonNull(this.getClass().getClassLoader().getResource(BootStrap.DEFAULT_PATH)).getPath());
    }

    public void start() throws IOException {
        Service.getInstance().start();
    }

    /**
     * start
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        BootStrap bootStrap = new BootStrap();
        bootStrap.init();
        bootStrap.start();
    }

}
